<?php

namespace app\index\Model;

use think\Model;
use catetree\Catetree;

class TpGoods extends Model
{


    //获取相关推荐位id下商品信息
    public function getGoodsRecpos($recposID)
    {
        $GoodsRecpos = array();
        $_GoodsRecpos = db('tp_rec_item')->where(['recpos_id' => $recposID, 'value_type' => 1])->select();
        foreach ($_GoodsRecpos as $k => $v) {
            $arr = $this->where(['id' => $v['value_id']])->find();
            if ($arr) {
                $GoodsRecpos[] = $arr;
            }

        }
        return $GoodsRecpos;
    }


    //获取首页推荐主分类下所有子分类下的推荐商品的信息，在首页展示
    public function getIndexRecposGoods($cateID, $recposID)
    {
        $Catetree = new Catetree();
        $sonID = $Catetree->childrenids($cateID, db('tp_category'));//注意$sonID位静态数组
        $sonID[] = $cateID;
        //获取首页推荐的商品id
        $_recGoods = db('tp_rec_item')->where(['recpos_id' => $recposID, 'value_type' => 1])->select();
        $recGoods = array();
        foreach ($_recGoods as $k1 => $v1) {
            $recGoods[] = $v1['value_id'];
        }
        $map['category_id'] = array('IN', $sonID);
        $map['id'] = array('IN', $recGoods);
        //获取首页推荐下所有分类的推荐商品的信息
        $RecposGoods = db('tp_goods')->where($map)->limit(6)->order('id DESC')->select();
        return $RecposGoods;

    }


//获取会员价格，在商品详情页展示
    public function getMemberPrice($goods_id)
    {
        $shopInfo = $this->where(array('id' => $goods_id))->find();
        $memberId = session('memberId');
        $memberRate = session('memberRate');
        if (session('memberId')) {//存在会员级别(登录的情况)
            $memberPrice = db('tp_member_price')->where(array('mlevel_id' => $memberId, 'goods_id' => $goods_id))->field('mprice')->find();
            if ($memberPrice) {//当存在会员价格的时候，使用会员价格
                $price = $memberPrice['mprice'];

            } else {//不存在会员价格的时候，使用折扣率
                $memberRate = $memberRate / 100;
                $price = $shopInfo['shop_price'] * $memberRate;
            }
        } else {//不存在会员级别(未登录的情况)
            $price = $shopInfo['shop_price'];
        }
        return $price;
    }

    //获取商品本店价
    public function getGoodsShopPrice($goods_id)
    {

        $goodsInfo = $this->field('shop_price')->find($goods_id);
        return $goodsInfo['shop_price'];
    }


    //获取分类页商品的信息
    public function goodsInfo($categoryId)
    {
        $Catetree = new Catetree();
        $cateIds = array();
        $cateIds = $Catetree->childrenids($categoryId, db('tp_category'));
        $cateIds[] = intval($categoryId);


        $where = array(
            'g.on_sale' => 1,
            'category_id' => ['in', $cateIds]
        );

        //品牌筛选
        $brandId = input('brandId');
        if ($brandId) {
            $where['brand_id'] = $brandId;
        }

        //价格筛选
        $price = explode('-', input('price'));
        if (input('price')) {
            $where['shop_price'] = ['between', [$price[0], $price[1]]];
        }

        $orderBy = 'shop_price';
        $orderWay = 'ASC';

        //销量排序
        if (input('orderBy')) {
            $orderBy = input('orderBy');
        }
        if (input('orderWay')) {
            $orderWay = input('orderWay');
        }


        $goodsRes = db('tp_goods')->alias('g')->field('g.id,g.goods_name,g.or_thumb,g.big_thumb,g.shop_price,IFNULL(sum(og.goods_num),0) xl,  IFNULL((select count(goods_id) from tp_comment c where c.goods_id=g.id),0) pl , (select group_concat(gp.big_photo) from tp_goods_photo gp where gp.goods_id=g.id) photo,(select group_concat(r.rec_name) from tp_recpos r where rec_type=1 and id in(select ri.recpos_id from tp_rec_item ri where value_type=1 and ri.value_id=g.id )) recName')->join('tp_order_goods og', 'g.id = og.goods_id And og.order_id IN (select id from tp_order where pay_status=1)', 'LEFT')->where($where)->group('g.id')->order($orderBy, $orderWay)->select();
        

        return $goodsRes;

    }


}
